package com.breaker.kata.leetcode;

public class LongestPalindrome {

    public String longestPalindrome(String s) {
        String first = s.substring(0, 1);
        String max = s.substring(0, 1);
        int start = 0;

        for (int i = 1; i < s.length(); i++) {
            for (int j = i; j < s.length(); j++) {
                if (String.valueOf(s.charAt(j)).equals(first)) {
                    String string = s.substring(start, j + 1);
                    if (string.equals(reverseString(string))) {
                        max = max.length() >= string.length() ? max : string;
                    }
                }
            }
            first = String.valueOf(s.charAt(i));
            start = i;
        }
        return max;
    }

    private String reverseString(String string) {
        StringBuilder input = new StringBuilder();
        input.append(string);
        input.reverse();
        return input.toString();
    }
}
